package com.icim.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.icim.entity.Ret_cus_prd_indt;
import com.icim.entity.Ret_cus_prd_indt_ablist;
import com.icim.entity.Ret_cus_so;
import com.icim.entity.Ret_prd_info;

@Repository
public class RetCusPrdIndtAblistDao {
    @Resource
    private JdbcTemplate icimJdbcTemplate;

    public void saveBatch(List<Ret_cus_prd_indt_ablist> retCusPrdIndtAbList) {
        List<Object[]> batch = new ArrayList<Object[]>();

        for (Ret_cus_prd_indt_ablist retCusIndtAb : retCusPrdIndtAbList) {
            batch.add(new Object[] { retCusIndtAb.getVdrPrdSeqId(), retCusIndtAb.getCode(), retCusIndtAb.getCodeDesc() });
        }

        StringBuffer sqlBuffer = new StringBuffer("INSERT INTO RET_CUS_PRD_INDT_ABLIST(");
        sqlBuffer.append("VDR_PRD_SEQ_ID,CODE,CODE_DESC");
        sqlBuffer.append(") VALUES(");
        sqlBuffer.append("?,?,?");
        sqlBuffer.append(")");

        icimJdbcTemplate.batchUpdate(sqlBuffer.toString(), batch);
    };

    public List<Ret_cus_prd_indt_ablist> getAbList(String vdrPrdSeqId) {
        String sql = "SELECT CODE,CODE_DESC FROM RET_CUS_PRD_INDT_ABLIST  WHERE  VDR_PRD_SEQ_ID  = ? ";
        return icimJdbcTemplate.query(sql, new Object[] { vdrPrdSeqId }, new RowMapper<Ret_cus_prd_indt_ablist>() {

            @Override
            public Ret_cus_prd_indt_ablist mapRow(ResultSet rs, int rowNum) throws SQLException {
                Ret_cus_prd_indt_ablist retCusPrdIndtAblist = new Ret_cus_prd_indt_ablist();
		retCusPrdIndtAblist.setCode(rs.getString("CODE"));
                retCusPrdIndtAblist.setCodeDesc(rs.getString("CODE_DESC"));
                return retCusPrdIndtAblist;
            }
        });
    }
}
